// pages/details/details.js
const db = wx.cloud.database()
const cont = db.collection('jihua');
const cont2 = db.collection('daily')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    realityday: '',
    trueday: '',
    day: '',
    whenday: '',
    whenday2: '',
    guangchantab: '',
    realitydayjudge: '',
    recycleday: '',
    finish: '',
    jihua: [],
    img: "/images/1_画板 1.png",
    ratio: "",
    openid: '',
    dailyValue: '',
    dailywhenday1: '',
    dailywhenday2: '',
    dakatab: '',
    dakatab2: '',
    userimg1: '',
    userjudge: '',
    flag: '',
    flagall: "",
    kaiguang: '',
    jinyong: '',
    who: "",
    continuationday: '',
    number: ""
  },

  getdailyValue: function (e) {
    var dailyValue = this.data.dailyValue
    this.setData({
      dailyValue: e.detail.value
    });
    console.log(dailyValue)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("id123", options)
    var id = options
    // console.log(id)
    this.setData({
      id: id
    })
    console.log("456", this.data.id)
    db.collection("jihua").doc("133e253361c098a2014d34b24c6bfc2d").get({
      success: res => {
        var flagall = res.data.flag
        this.setData({
          flagall: flagall
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {



  },

  // fenzhuang(){
  //   let that = this;


  // },

  switch: function () {
    if (this.data.flag == false) {
      this.data.flag = true
    } else {
      this.data.flag = false
    }
    this.setData({
      flag: this.data.flag
    })
    console.log(this.data.flag)
    wx.cloud.callFunction({
      name: 'addtrueday',
      data: {
        dataId: this.data.id.id,
        flag: this.data.flag
      },
      success: res => {
        console.log(res)
      },

    })
  },


  getOpenid() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        console.log('云函数获取到的openid: ', res.result.openid)
        var openid = res.result.openid;
        that.setData({
          openid: openid
        })
        console.log(this.data.openid)

        console.log("实际", this.data.realityday)
        console.log("计划", this.data.day)
        var day = Number(this.data.day)
        var ratio = this.data.realityday / day
        if (this.data.userjudge == "" || this.data.userjudge == null) {
          console.log("广场", ratio)
          if (ratio > 0.2 || ratio == 0.2) {
            this.data.guangchantab = false
            this.data.flag = false
            this.data.jinyong = true
          } else {
            this.data.guangchantab = true
            this.data.jinyong = false
          }
          var jinyong = this.data.jinyong
          var guangchantab = this.data.guangchantab
          var flag = this.data.flag
          this.setData({
            guangchantab: guangchantab,
            flag: flag,
            jinyong: jinyong,
            userimg1: true,
          })
          console.log("guangchangall", this.data.guangchantab)
          console.log("flagll", this.data.flag)
          console.log("huanzgehao")
        } else {
          this.data.guangchantab = false
          this.data.flag = false
          var guangchantab = this.data.guangchantab
          var flag = this.data.flag
          this.setData({
            userimg1: false,
            guangchantab: guangchantab,
            flag: flag
          })
        }
        console.log(this.data.userimg1)
        wx.cloud.callFunction({
          name: 'addtrueday',
          data: {
            dataId: this.data.id.id,
            userimg1: this.data.userimg1,
            guangchantab: guangchantab,
            flag: flag,
            jinyong: jinyong
          },
          success: res => {
            console.log(res)
            console.log("检查", this.data.openid)
            db.collection("user").where({
              _openid: openid
            }).get({
              success: res => {
                var number = res.data[0].number
                this.setData({
                  number: number
                })
                console.log("user的number", this.data.number)
              }
            })
          },

        })
      }

    })

  },

  // getUse() {
  //   let that = this;
  //   wx.cloud.callFunction({
  //     name: 'getUse',
  //     complete: res => {
  //       console.log('云函数获取到的openid: ', res.result.use)
  //       var use = res.result.use;
  //       that.setData({
  //         use: use
  //       })
  //       console.log(this.data.use)
  //     }
  //   })
  // } ,


  daka: function () {
    // if(this.data.jihua.openid == this.data.jihua.openid){
    var D = new Date();
    if (D.getMonth() < 9) {
      taskStartTime = D.getFullYear() + "-0" + (D.getMonth() + 1) + "-"
    } else {
      taskStartTime = D.getFullYear() + "-" + (D.getMonth() + 1) + "-"
    }
    if (D.getDate() < 10) {
      taskStartTime += "0" + D.getDate()
    } else {
      taskStartTime += D.getDate()
    }
    console.log(taskStartTime)
    var H = new Date(); //获取当前日期的前一天日期
    if (H.getMonth() < 9) {
      taskStartTime1 = H.getFullYear() + "-0" + (H.getMonth() + 1) + "-"
    } else {
      taskStartTime1 = H.getFullYear() + "-" + (H.getMonth() + 1) + "-"
    }
    if (H.getDate() < 10) {
      taskStartTime1 += "0" + (H.getDate() - 1)
    } else {
      taskStartTime1 += (H.getDate() - 1)
    }
    console.log(taskStartTime1) //获取当前日期的前一天日期
    console.log(this.data.trueday)
    var trueday = Number(this.data.trueday)
    var openid1 = this.data.jihua._openid
    console.log(D)
    console.log(openid1)
    if (this.data.openid != openid1) {
      if (taskStartTime != this.data.whenday) {
        if (taskStartTime1 == this.data.whenday && this.data.trueday == 1) {
          this.data.continuationday += 1
        } else if (taskStartTime1 == this.data.whenday && this.data.trueday == 0) {
          this.data.continuationday = this.data.continuationday
        } else if (taskStartTime1 != this.data.whenday) {
          if (this.data.whenday == "" && this.data.trueday == 1) {
            this.data.continuationday += 1
          } else if (this.data.whenday == "" && this.data.trueday == 0) {
            this.data.continuationday = this.data.continuationday
          } else if (this.data.whenday != "" && this.data.trueday == 1) {
            this.data.continuationday = 1
          } else if (this.data.whenday != "" && this.data.trueday == 0) {
            this.data.continuationday = 0
          }
        }
        var continuationday = Number(this.data.continuationday)
        this.setData({
          continuationday: continuationday
        })
        console.log("连续天数", continuationday)
        wx.cloud.callFunction({
          name: 'addtrueday',
          data: {
            dataId: this.data.id.id,
            continuationday: continuationday
          },
          success: res => {
            console.log(res)
          },
        })
        if (this.data.jihua.trueday == 'null') {
          this.data.jihua.trueday = this.data.trueday += 1
          console.log(trueday)
        } else {
          this.data.jihua.trueday = this.data.trueday += 1
        }
        this.setData({
          trueday: this.data.jihua.trueday,
          whenday: taskStartTime,
          dakatab2: true,
        })
        console.log(this.data.whenday)
        console.log(trueday)
        var realityday = Number(this.data.jihua.realityday)
        var trueday3 = trueday += 1
        if (this.data.userjudge == "" || this.data.userjudge == null) {
          if (trueday3 == 1) {
            this.data.jihua.realityday = this.data.realityday += 1
            console.log(realityday)
          } else {
            this.data.realityday = this.data.jihua.realityday
          }
        } else {
          if (trueday3 == 2) {
            this.data.jihua.realityday = this.data.realityday += 1
            console.log(realityday)
          } else {
            this.data.realityday = this.data.jihua.realityday
          }
        }
        this.setData({
          realityday: this.data.jihua.realityday
        })
        console.log(this.data.jihua.realityday)
        //订阅消息
        console.log("订阅测试", this.data, jihua)
        var name = this.data.jihua.username
        var day = this.data.jihua.realityday
        console.log("天数", day)
        var jihua = this.data.jihua.plan
        var openid = this.data.openid
        wx.requestSubscribeMessage({
          tmplIds: ["4LQ-BeeKWUkBrN9FVzCKbHZ0mt7hn3pP5NdGCzpjcmc"],
          success: res => {
            if (res["4LQ-BeeKWUkBrN9FVzCKbHZ0mt7hn3pP5NdGCzpjcmc"] === 'accept') {
              console.log("授权")
              // 调用云函数subscribe
              wx.cloud.callFunction({
                  name: 'addCourseMsg',
                  data: {
                    data: {
                      "thing9": {
                        "value": jihua
                      },
                      "thing6": {
                        "value": this.data.jihua.realityday
                      },
                      "thing14": {
                        "value": name
                      },
                    },
                    id: this.data.id.id,
                    openid: openid,
                    templateId: "4LQ-BeeKWUkBrN9FVzCKbHZ0mt7hn3pP5NdGCzpjcmc",
                  },
                })
                .then(() => {
                  wx.showToast({
                    title: '订阅成功，我们将在明天提醒您打卡',
                    icon: "none"
                  });
                })
                .catch(() => {
                  // dothing...
                });
            }
          },
        });
        //以上是订阅
        wx.cloud.callFunction({
          name: 'addtrueday',
          data: {
            dataId: this.data.id.id,
            realityday: this.data.realityday
          },
          success: res => {
            console.log(res)
          },

        })
        var day = Number(this.data.day)
        var ratio = this.data.realityday / day
        var ratio2 = (((ratio.toFixed(4) * 10000) / 100.00).toFixed(2) + "%")
        console.log(ratio2)
        if (ratio < 0.2) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/树木素材/1_画板 1.png"
        } else if (ratio > 0.2 && ratio < 0.4 || ratio == 0.2) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/树木素材/3_画板 1.png"
        } else if (ratio > 0.4 && ratio < 0.6 || ratio == 0.4) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/树木素材/2_画板 1.png"
        } else if (ratio > 0.6 && ratio < 0.8 || ratio == 0.6) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/80-01.png"
        } else if (ratio > 0.8 && ratio < 1.1 || ratio == 0.8) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/100-01.png"
        }
        console.log(this.data.img)
        this.setData({
          ratio: ratio2,
          img: this.data.img
        })
        wx.showToast({
          title: '签到成功！',
          icon: 'success',
          duration: 1200,
          mask: true
        })
        db.collection("jihua").where({
          _openid: this.data.openid,
          finish:false
        }).orderBy('continuationday', 'desc').get({
          success: res => {
            console.log("测试赛所所事实上", res.data[0].continuationday)
            var daynumber = res.data[0].continuationday
            if (daynumber+1 == this.data.continuationday) {
              daynumber = daynumber+=1
            }
            this.setData({
              number: daynumber
            })
            console.log("dasdasdasdasdasdsa", this.data.number)
            var number = this.data.number
            wx.cloud.callFunction({
              name: 'addpaiming',
              data: {
                openid: this.data.openid,
                number: number
              },
              success: res => {
                console.log("排行测试成功", res)
              },
            })
          }
        })
        var day = Number(this.data.day)

        if (this.data.realityday == day) {
          this.setData({
            finish: true,
            who: openid
          })
          wx.showToast({
            title: '计划完成！',
            icon: 'success',
            duration: 1200,
            mask: true
          })
          setTimeout(function () {
            wx.switchTab({
              url: '/pages/shouye/shouye',
            })
          }, 1800);
        }
        wx.cloud.callFunction({
          // 云函数名称【刚刚创建的云函数文件的名字】
          name: 'addtrueday',
          // 传给云函数的参数　　 // dataId 为我要修改的 users 集合内数据的 _id ； day 为要修改的内容
          data: {
            dataId: this.data.id.id,
            trueday: this.data.trueday,
            whenday: this.data.whenday,
            dakatab2: this.data.dakatab2,
            finish: this.data.finish,
            who: this.data.who
          },
          success: res => {
            console.log(res)
          },

        })

      } else {
        wx.showToast({
          title: '今日打卡已完成',
          icon: 'loading',
          duration: 1200,
          mask: true
        })
      }
    } else {
      if (taskStartTime != this.data.whenday2) {
        if (this.data.userjudge == "" || this.data.userjudge == null) {
          if (taskStartTime1 == this.data.whenday2) {
            this.data.continuationday += 1
          } else {
            if (this.data.whenday2 == "") {
              this.data.continuationday += 1
            } else {
              this.data.continuationday = 1
            }
          }
        } else {
          if (taskStartTime1 == this.data.whenday2 && this.data.trueday == 1) {
            this.data.continuationday += 1
          } else if (taskStartTime1 == this.data.whenday2 && this.data.trueday == 0) {
            this.data.continuationday = this.data.continuationday
          } else if (taskStartTime1 != this.data.whenday2) {
            if (this.data.whenday2 == "" && this.data.trueday == 1) {
              this.data.continuationday += 1
            } else if (this.data.whenday2 == "" && this.data.trueday == 0) {
              this.data.continuationday = this.data.continuationday
            } else if (this.data.whenday2 != "" && this.data.trueday == 1) {
              this.data.continuationday = 1
            } else if (this.data.whenday2 != "" && this.data.trueday == 0) {
              this.data.continuationday = 0
            }
          }
        }
        var continuationday = Number(this.data.continuationday)
        this.setData({
          continuationday: continuationday
        })
        console.log("连续天数", continuationday)
        wx.cloud.callFunction({
          name: 'addtrueday',
          data: {
            dataId: this.data.id.id,
            continuationday: continuationday
          },
          success: res => {
            console.log(res)
          },
        })
        if (this.data.jihua.trueday == 'null') {
          this.data.jihua.trueday = this.data.trueday += 1
          console.log(this.data.jihua.trueday)
        } else {
          this.data.jihua.trueday = this.data.trueday += 1
        }
        this.setData({
          trueday: this.data.jihua.trueday,
          whenday2: taskStartTime,
          dakatab: true,
        })
        console.log(trueday)
        console.log(this.data.whenday2)
        var realityday = Number(this.data.jihua.realityday)
        var trueday3 = trueday += 1
        if (this.data.userjudge == "" || this.data.userjudge == null) {
          if (trueday3 == 1) {
            this.data.jihua.realityday = this.data.realityday += 1
            console.log(realityday)
          } else {
            this.data.realityday = this.data.jihua.realityday
          }
        } else {
          if (trueday3 == 2) {
            this.data.jihua.realityday = this.data.realityday += 1
            console.log(realityday)
          } else {
            this.data.realityday = this.data.jihua.realityday
          }
        }
        this.setData({
          realityday: this.data.jihua.realityday
        })
        console.log(this.data.jihua.realityday)
        //订阅消息
        console.log("订阅测试", this.data, jihua)
        var name = this.data.jihua.mename
        var day = this.data.jihua.realityday
        console.log("天数", day)
        var jihua = this.data.jihua.plan
        var openid = this.data.openid
        wx.requestSubscribeMessage({
          tmplIds: ["4LQ-BeeKWUkBrN9FVzCKbHZ0mt7hn3pP5NdGCzpjcmc"],
          success: res => {
            if (res["4LQ-BeeKWUkBrN9FVzCKbHZ0mt7hn3pP5NdGCzpjcmc"] === 'accept') {
              console.log("授权")
              // 调用云函数subscribe
              wx.cloud.callFunction({
                  name: 'addCourseMsg',
                  data: {
                    data: {
                      "thing9": {
                        "value": jihua
                      },
                      "thing6": {
                        "value": this.data.jihua.realityday
                      },
                      "thing14": {
                        "value": name
                      },
                    },
                    id: this.data.id.id,
                    openid: openid,
                    templateId: "4LQ-BeeKWUkBrN9FVzCKbHZ0mt7hn3pP5NdGCzpjcmc",
                  },
                })
                .then(() => {
                  wx.showToast({
                    title: '订阅成功，我们将在明天提醒您打卡',
                    icon: "none"
                  });
                })
                .catch(() => {
                  // dothing...
                });
            }
          },
        });
        //以上是订阅
        wx.cloud.callFunction({
          name: 'addtrueday',
          data: {
            dataId: this.data.id.id,
            realityday: this.data.realityday
          },
          success: res => {
            console.log(res)
          },

        })
        var day = Number(this.data.day)
        var ratio = this.data.realityday / day
        var ratio2 = (((ratio.toFixed(4) * 10000) / 100.00).toFixed(2) + "%")
        console.log(ratio2)
        if (ratio < 0.2) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/树木素材/1_画板 1.png"
        } else if (ratio > 0.2 && ratio < 0.4 || ratio == 0.2) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/树木素材/3_画板 1.png"
        } else if (ratio > 0.4 && ratio < 0.6 || ratio == 0.4) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/树木素材/2_画板 1.png"
        } else if (ratio > 0.6 && ratio < 0.8 || ratio == 0.6) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/80-01.png"
        } else if (ratio > 0.8 && ratio < 1.1 || ratio == 0.8) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/100-01.png"
        }
        console.log(this.data.img)
        var guangchantab = this.data.guangchantab
        console.log("广场", ratio)
        if (ratio > 0.2 || ratio == 0.2) {
          this.data.guangchantab = false
          this.data.flag = false
          this.data.jinyong = true
          console.log(guangchantab)
        } else {
          if (this.data.userjudge == "" || this.data.userjudge == null) {
            this.data.guangchantab = true
            this.data.jinyong = false
          } else {
            this.data.guangchantab = false
          }
        }
        var guangchantab = this.data.guangchantab
        var flag = this.data.flag
        var jinyong = this.data.jinyong
        this.setData({
          ratio: ratio2,
          img: this.data.img,
          guangchantab: guangchantab,
          flag: flag,
          jinyong: jinyong
        })
        wx.showToast({
          title: '签到成功！',
          icon: 'success',
          duration: 1200,
          mask: true
        })
        db.collection("jihua").where({
          _openid: this.data.openid,
          finish:false
        }).orderBy('continuationday', 'desc').get({
          success: res => {
            console.log("测试赛所所事实上", res.data[0].continuationday)
            var daynumber = res.data[0].continuationday
            if (daynumber+1 == this.data.continuationday) {
              daynumber = daynumber+=1
            }
            this.setData({
              number: daynumber
            })
            console.log("dasdasdasdasdasdsa", this.data.number)
            var number = this.data.number
            wx.cloud.callFunction({
              name: 'addpaiming',
              data: {
                openid: this.data.openid,
                number: number
              },
              success: res => {
                console.log("排行测试成功", res)
              },
            })
          }
        })
        console.log(this.data.realityday)
        var day = Number(this.data.day)
        if (this.data.realityday == day) {
          this.setData({
            finish: true,
            who: openid
          })
          wx.showToast({
            title: '计划完成！',
            icon: 'success',
            duration: 1200,
            mask: true
          })
          setTimeout(function () {
            wx.switchTab({
              url: '/pages/shouye/shouye',
            })
          }, 1800);
        }
        wx.cloud.callFunction({
          name: 'addtrueday',
          data: {
            dataId: this.data.id.id,
            trueday: this.data.trueday,
            whenday2: this.data.whenday2,
            dakatab: this.data.dakatab,
            finish: this.data.finish,
            guangchantab: guangchantab,
            flag: flag,
            jinyong: jinyong,
            who: this.data.who
          },
          success: res => {
            console.log(res)
          },

        })

      } else {
        wx.showToast({
          title: '今日打卡已完成',
          icon: 'loading',
          duration: 1200,
          mask: true
        })
      }


    }


    // }else{
    //   if (D != this.data.whenday) {
    //     if (this.data.jihua.trueday == 'null') {
    //       this.data.jihua.trueday = trueday += 1
    //       console.log(this.data.jihua.trueday)
    //     } else {
    //       this.data.jihua.trueday = this.data.jihua.trueday += 1
    //     }
    //     this.setData({
    //       trueday: this.data.jihua.trueday,
    //       whenday: D,
    //       finish:true
    //     })
    //     console.log(this.data.jihua.trueday)
    //     console.log(this.data.whenday)
    //     console.log(this.data.finish)
    //     wx.showToast({
    //       title: '签到成功！',
    //       icon: 'success',
    //       duration: 1200,
    //       mask: true
    //     })
    //     wx.cloud.callFunction({
    // 云函数名称【刚刚创建的云函数文件的名字】
    // name: 'addtrueday',
    // 传给云函数的参数　　 // dataId 为我要修改的 users 集合内数据的 _id ； day 为要修改的内容
    //     data: {
    //       dataId: this.data.id.id,
    //       trueday: this.data.trueday,
    //       whenday: this.data.whenday,
    //       finish:this.data.finish
    //     },
    //     success: function (res) {
    //       console.log(res)
    //     },

    //   })

    // } else {
    //   wx.showToast({
    //     title: '今日打卡已完成',
    //     icon: 'loading',
    //     duration: 1200,
    //     mask: true
    //   })
    // }
    // }

    // if(this.data.jihua.use == this.data.jihua.use){
    //   var D = (new Date()).toLocaleDateString();
    //   var trueday = Number(this.data.jihua.trueday)
    //   console.log(D)
    //     if (D != this.data.whenday2) {
    //       if (this.data.jihua.trueday == 'null') {
    //         this.data.jihua.trueday = trueday += 1
    //         console.log(this.data.jihua.trueday)
    //       } else {
    //         this.data.jihua.trueday = this.data.jihua.trueday += 1
    //       }
    //       this.setData({
    //         trueday: this.data.jihua.trueday,
    //         whenday2: D
    //       })
    //       console.log(this.data.jihua.trueday)
    //       console.log(this.data.whenday2)
    //       wx.showToast({
    //         title: '签到成功！',
    //         icon: 'success',
    //         duration: 1200,
    //         mask: true
    //       })
    //       wx.cloud.callFunction({
    //         // 云函数名称【刚刚创建的云函数文件的名字】
    //         name: 'addtrueday',
    //         // 传给云函数的参数　　 // dataId 为我要修改的 users 集合内数据的 _id ； day 为要修改的内容
    //         data: {
    //           dataId: this.data.id.id,
    //           trueday: this.data.trueday,
    //           whenday2: this.data.whenday2
    //         },
    //         success: function (res) {
    //           console.log(res)
    //         },

    //       })

    //     } else {
    //       wx.showToast({
    //         title: '今日打卡已完成',
    //         icon: 'loading',
    //         duration: 1200,
    //         mask: true
    //       })
    //     }

    // }
  },

  all: function () {
    var id = this.data.id.id
    wx.navigateTo({
      url: '/pages/all/all?id=' + id,
    })
  },

  savedaily: function () {
    var openid2 = this.data.jihua._openid
    var jihuaid = this.data.id.id
    var F = new Date();
    if (F.getMonth() < 9) {
      taskStartTime = F.getFullYear() + "-0" + (F.getMonth() + 1) + "-"
    } else {
      taskStartTime = F.getFullYear() + "-" + (F.getMonth() + 1) + "-"
    }
    if (F.getDate() < 10) {
      taskStartTime += "0" + F.getDate()
    } else {
      taskStartTime += F.getDate()
    }
    console.log(taskStartTime)
    if (this.data.dailyValue == "" || this.data.dailyValue == "None") {
      wx.showToast({
        title: '请输入日志',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.openid == openid2) {
      if (taskStartTime != this.data.dailywhenday1) {
        db.collection('daily').add({
          data: {
            dailytext: this.data.dailyValue,
            jihuaid: jihuaid,
            dailytime: taskStartTime,
            firstopenid: openid2
          },
          success: res => {
            console.log(res._id)
            // var id = res._id
            // this.setData({
            //   id: id
            // })
          }
        })
        this.setData({
          dailywhenday1: taskStartTime,
          dailyValue:""
        })
        console.log(this.data.dailywhenday1)
        wx.showToast({
          title: '保存成功！',
          icon: 'success',
          duration: 1200,
          mask: true
        })
        wx.cloud.callFunction({
          name: 'addtrueday',
          data: {
            dataId: this.data.id.id,
            dailywhenday1: this.data.dailywhenday1
          },
          success: res => {
            console.log(res)
          },

        })
      } else {
        this.setData({
          dailyValue:""
        })
        wx.showToast({
          title: '今日日志已填写',
          icon: 'loading',
          duration: 1200,
          mask: true
        })
      }
    } else {
      if (taskStartTime != this.data.dailywhenday2) {
        db.collection('daily').add({
          data: {
            dailytext: this.data.dailyValue,
            jihuaid: jihuaid,
            dailytime: taskStartTime,
            firstopenid: openid2
          },
          success: res => {
            console.log(res._id)
            // var id = res._id
            // this.setData({
            //   id: id
            // })
          }
        })
        this.setData({
          dailywhenday2: taskStartTime,
          dailyValue:""
        })
        console.log(this.data.dailywhenday2)
        wx.showToast({
          title: '保存成功！',
          icon: 'success',
          duration: 1200,
          mask: true
        })
        wx.cloud.callFunction({
          name: 'addtrueday',
          data: {
            dataId: this.data.id.id,
            dailywhenday2: this.data.dailywhenday2
          },
          success: res => {
            console.log(res)
          },

        })
      } else {
        this.setData({
          dailyValue:""
        })
        wx.showToast({
          title: '今日日志已填写',
          icon: 'loading',
          duration: 1200,
          mask: true
        })
      }

    }




  },

  shanchu: function () {
    var openid1 = this.data.jihua._openid
    console.log(openid1)
    wx.showModal({
      title: '删除',
      content: '确认后会删除该计划！',
      success: res => {
        if (res.confirm) {
          if (this.data.openid == openid1) {
            cont.doc(this.data.id.id).remove({
              success: res => {
                console.log(res)
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 1200,
                  mask: true
                })
              }
            });
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/shouye/shouye',
              })
            }, 1800);
          } else {
            var E = new Date();
            if (E.getMonth() < 9) {
              taskStartTime = E.getFullYear() + "-0" + (E.getMonth() + 1) + "-"
            } else {
              taskStartTime = E.getFullYear() + "-" + (E.getMonth() + 1) + "-"
            }
            if (E.getDate() < 10) {
              taskStartTime += "0" + E.getDate()
            } else {
              taskStartTime += E.getDate()
            }
            console.log(taskStartTime)
            var trueday = Number(this.data.trueday)
            var realityday = Number(this.data.realityday)
            if (taskStartTime == this.data.whenday) {
              if (this.data.trueday == 2) {
                this.data.jihua.trueday = this.data.trueday -= 1
                console.log(trueday)
                this.data.jihua.realityday = this.data.realityday -= 1
                console.log(realityday)
              } else {
                this.data.jihua.trueday = this.data.trueday -= 1
                this.data.jihua.realityday = this.data.realityday
              }
            } else {
              this.data.jihua.trueday = this.data.trueday
              this.data.jihua.realityday = this.data.realityday
            }
            this.setData({
              trueday: this.data.jihua.trueday,
              realityday: this.data.jihua.realityday
            })
            wx.cloud.callFunction({
              name: 'delet',
              data: {
                dataId: this.data.id.id,
                realityday: this.data.realityday,
                trueday: this.data.trueday,
                dailywhenday2: "",
                dakatab2: false,
                hezuo: "",
                use: "",
                userimg: "",
                userimg1: true,
                userjudge: "",
                username: "",
                whenday: ""
              },
              success: res => {
                console.log(res)
              }
            });
            console.log(this.data.id.id)
            console.log(this.data.openid)
            db.collection("daily").where({
              jihuaid: this.data.id.id,
              _openid: this.data.openid,
            }).remove({
              success: res => {
                console.log(res)
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 1200,
                  mask: true
                })
                setTimeout(function () {
                  wx.switchTab({
                    url: '/pages/shouye/shouye',
                  })
                }, 1800);
              }

            })



          }
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOpenid();
    cont.doc(this.data.id.id).get({
      success: res => {
        var trueday = res.data.trueday
        var realityday = res.data.realityday
        var whenday = res.data.whenday
        var whenday2 = res.data.whenday2
        var dailywhenday1 = res.data.dailywhenday1
        var dailywhenday2 = res.data.dailywhenday2
        var day = res.data.day
        var recycleday = res.data.recycleday
        var finish = res.data.finish
        var dakatab = res.data.dakatab
        var dakatab2 = res.data.dakatab2
        var userjudge = res.data.userjudge
        var userimg1 = res.data.userimg1
        var jinyong = res.data.jinyong
        var continuationday = res.data.continuationday
        var guangchantab = res.data.guangchantab
        var flag = res.data.flag
        var ratio = realityday / day
        var ratio2 = (((ratio.toFixed(4) * 10000) / 100.00).toFixed(2) + "%")
        console.log(ratio2)
        console.log(recycleday)
        if (ratio < 0.2) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/树木素材/1_画板 1.png"
        } else if (ratio > 0.2 && ratio < 0.4 || ratio == 0.2) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/树木素材/3_画板 1.png"
        } else if (ratio > 0.4 && ratio < 0.6 || ratio == 0.4) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/树木素材/2_画板 1.png"
        } else if (ratio > 0.6 && ratio < 0.8 || ratio == 0.6) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/80-01.png"
        } else if (ratio > 0.8 && ratio < 1.1 || ratio == 0.8) {
          this.data.img = "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/100-01.png"
        }
        console.log(this.data.img)
        this.setData({
          jihua: res.data,
          trueday: trueday,
          realityday: realityday,
          whenday: whenday,
          whenday2: whenday2,
          dailywhenday1: dailywhenday1,
          dailywhenday2: dailywhenday2,
          day: day,
          recycleday: recycleday,
          userimg1: userimg1,
          jinyong: jinyong,
          guangchantab: guangchantab,
          continuationday: continuationday,
          finish: finish,
          dakatab: dakatab,
          dakatab2: dakatab2,
          flag: flag,
          userjudge: userjudge,
          img: this.data.img,
          ratio: ratio2
        })
        console.log(this.data.jihua)
        console.log(this.data.day)
        console.log(this.data.finish)
        console.log(this.data.recycleday)
        console.log(this.data.userimg1)
        var E = new Date();
        if (E.getMonth() < 9) {
          taskStartTime = E.getFullYear() + "-0" + (E.getMonth() + 1) + "-"
        } else {
          taskStartTime = E.getFullYear() + "-" + (E.getMonth() + 1) + "-"
        }
        if (E.getDate() < 10) {
          taskStartTime += "0" + E.getDate()
        } else {
          taskStartTime += E.getDate()
        }
        console.log(taskStartTime)
        var trueday = Number(this.data.jihua.trueday)
        console.log(this.data.recycleday)
        console.log(trueday)
        console.log(this.data.trueday)
        if (taskStartTime != this.data.recycleday) {
          this.data.trueday = trueday = 0
          this.setData({
            trueday: this.data.trueday,
            recycleday: taskStartTime,
            dakatab: false,
            dakatab2: false
          })
          console.log(this.data.trueday)
          console.log(trueday)
          console.log(this.data.dakatab)
          wx.cloud.callFunction({
            name: 'addtrueday',
            data: {
              dataId: this.data.id.id,
              trueday: this.data.trueday,
              recycleday: this.data.recycleday,
              dakatab: this.data.dakatab,
              dakatab2: this.data.dakatab2,
            },
            success: res => {
              console.log(res)
              console.log(trueday)
            },

          })
        } else {
          this.data.trueday = this.data.jihua.trueday
          console.log(this.data.trueday)
          console.log(this.data.jihua.trueday)
          this.setData({
            trueday: this.data.trueday
          })
        }
        console.log(this.data.trueday)
        console.log("shg", this.data.flag)
        if (this.data.flag == false) {
          this.data.kaiguang = kaiguang = false
          console.log(this.data.kaiguang)
        } else {
          this.data.kaiguang = kaiguang = true
          console.log(this.data.kaiguang)
        }
        console.log("abd", this.data.kaiguang)
        var kaiguan = this.data.kaiguang
        this.setData({
          kaiguang: kaiguan
        })
        console.log("最终", this.data.kaiguang)
      }
    })
    wx.stopPullDownRefresh();
    // this.getUse();







    // var trueday = this.data.jihua.trueday
    // this.setData({
    //   trueday:trueday
    // })
    // var that = this;
    // var jihua = wx.getStorage({
    //   key: 'FirstTime',
    //   success: function (res) {
    //     that.setData({
    //       trueday: res.data
    //     })
    //   },
    // })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    console.log(this.data.jihua.mename)
    console.log(this.data.id.id)
    var id = this.data.id.id
    var name = this.data.jihua.mename
    var ta = this.data.jihua.username
    var openid = this.data.jihua._openid
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      console.log("id", ops.target)
      if (ops.target.id == 2) {
        return {
          title: name + '邀请你参与计划打卡',
          path: 'pages/particulars/particulars?id=' + id,
          imageUrl: '/images/1_画板 1.png', // 路径，传递参数到指定页面。
          success: res => {
            // 转发成功
            console.log("转发成功:" + JSON.stringify(res));
          },
          fail: res => {
            // 转发失败
            console.log("转发失败:" + JSON.stringify(res));
          }
        }
      }
      if (ops.target.id == 1) {
        if (openid == this.data.openid) {
          return {
            title: name + '提醒你计划打卡',
            path: 'pages/details/details?id=' + id,
            imageUrl: '/images/1_画板 1.png', // 路径，传递参数到指定页面。
            success: res => {
              // 转发成功
              console.log("转发成功:" + JSON.stringify(res));
            },
            fail: res => {
              // 转发失败
              console.log("转发失败:" + JSON.stringify(res));
            }
          }
        } else {
          return {
            title: ta + '提醒你计划打卡',
            path: 'pages/details/details?id=' + id,
            imageUrl: '/images/1_画板 1.png', // 路径，传递参数到指定页面。
            success: res => {
              // 转发成功
              console.log("转发成功:" + JSON.stringify(res));
            },
            fail: res => {
              // 转发失败
              console.log("转发失败:" + JSON.stringify(res));
            }
          }
        }

      }
    }


  }
})